package com.zhang.gulimall.product.web;

import com.zhang.gulimall.product.entity.CategoryEntity;
import com.zhang.gulimall.product.service.CategoryService;
import com.zhang.gulimall.product.vo.Catelog2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    CategoryService categoryService;

    @GetMapping({"/","index.html"})
    public String indexPage(Model model){
       List<CategoryEntity> categoryEntities = categoryService.listLevel1();
       model.addAttribute("categories",categoryEntities);
       return "index";
    }
    /**
     * 渲染二级、三级分类
     * @return
     */
    @ResponseBody
    @GetMapping({"/index/json/catalog.json"})
    public Map<String, List<Catelog2Vo>> getCatalogJson(){
        return categoryService.getCatalogJson();
    }

    @GetMapping("/level1")
    @ResponseBody
    public List<CategoryEntity> level1(){
        return categoryService.listLevel1();
    }
}
